#!/usr/bin/env ruby

#
#   Copyright 2011 Wade Alcorn wade@bindshell.net
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#

require 'rubygems'

puts "\nWelcome to the BeEF installer!"

if  RUBY_VERSION < '1.9'
  puts "\n"
  puts "Ruby version " + RUBY_VERSION + " is no longer supported. Please upgrade 1.9 or later."
  puts "\n"
  exit
end

puts "\nPlease make sure you have installed SQLite before proceeding.  For instructions on how to do this please see the README file"

# array of required gems - add to as needed (specify a version if needed eg "gem_name, =x.x.x")
$gems_required = ["ansi", "term-ansicolor", "dm-core", "json", "data_objects", "do_sqlite3", "sqlite3", "dm-sqlite-adapter", 
                  "parseconfig", "erubis", "dm-migrations", "librex, =0.0.52"]

# array of missing non-version specific gems installed
$gems_missing = Array.new

# array of missing version specific gems installed
$gems_missing_version = Array.new

# check all required gems (dependencies) are present
def dep_check
  $gems_required.each do |current_gem|
    begin
      if current_gem.include? ","
        tokens = current_gem.split(",")
        gem tokens[0], tokens[1]
      else
        gem current_gem
      end
    rescue Gem::LoadError
      if current_gem.include? ","
        $gems_missing_version << current_gem
      else
        $gems_missing << current_gem
      end
    end
  end
  if $gems_missing.length == 0 && $gems_missing_version.length == 0
    return true
  else
   return false
 end
end

# display install options
def display_opts
  puts "\n1) Install all required gems automatically\n" + "2) List required gems and exit so they can be installed manually\n" + "3) Exit installer\n\n"
  option = gets
  return option
end

# generate install command for missing gems
def install_command
  if (RUBY_PLATFORM =~ /linux/ or RUBY_PLATFORM =~ /darwin/) and Process.uid != 0
    cmd = "sudo gem install"
    $gems_missing.each do |current_gem|
      cmd = cmd + " #{current_gem}"
    end
    if $gems_missing_version.length != 0
      $gems_missing_version.each do |current_gem|
        if cmd == "sudo gem install"
          cmd = cmd + " #{current_gem}"
        else
          cmd = cmd + " && sudo gem install #{current_gem}"
        end
      end
    end
  else
    cmd = "gem install"
    $gems_missing.each do |current_gem|
      cmd = cmd + " #{current_gem}"
    end
    if $gems_missing_version.length != 0
      $gems_missing_version.each do |current_gem|
        if cmd == "gem install"
          cmd = cmd + " #{current_gem}"
        else
          cmd = cmd + " & gem install #{current_gem}"
        end
      end
    end
  end
  cmd = cmd.delete "," "'"
  cmd = cmd.gsub("=", "-v")
  cmd += " --no-rdoc --no-ri"
  return cmd
end

# install missing gems
def install_gems
  puts install_command + "\n"
  system(install_command)
end

dep_met = dep_check()

if dep_met == false
  puts "\nSome gems required by BEeF are not present on your system please select an option to continue:"
  option = display_opts
  while option != "1\n" and option != "2\n" and option != "3\n"
    puts "\nInvalid option entered, please select a valid option to continue:"
    option = display_opts
  end
  if option == "1\n"
    install_gems
  elsif option == "2\n"
    cmd = install_command
    puts "\nPlease run the following command to update and install all required gems:\n\n" + cmd + "\n\n"
  elsif option == "3\n"
    puts "\nExiting...\n\n"
  end
else
  puts "\nAll required gems are present - please run 'ruby beef' to start using BEeF\n\n"
  puts "\nThe Default username/password are beef/beef\n\n"
  puts "\nAll feedback welcome - http://beef.googlecode.com/\n\n"
end
